jQuery.fn.extend({
    jUpload: function(parametros) {
        var randomnumber = Math.floor(Math.random() * 11);
        var defaults = {
            dataType: "json",
            nome_form: "form_anexos_" + randomnumber,
            nome_botao: "Anexar",
            url: "",
            debug_display: false,
            carregando: true,
            carregando_txt: true,
            data: null,
            target_uploader: "iframe_anexos_" + randomnumber,
            success: function(a, b) {
            },
            error: function(a, b) {
            }
        };

        var option = $.extend(true, defaults, parametros);
        var campos_extras = "";
        var obj = this;
        var i_file = '<input type="file" class="adicionar_arquivo" name="file[]" multiple="multiple" />';
        if (option.data != null) {
            $.each(option.data, function(a, b) {
                campos_extras += '<input type="hidden" name="' + a + '" id="' + a + '" value="' + b + '" />\n';
            });
        }
        var _formulario = '<form method="post" id="' + option.nome_form + '" name="' + option.nome_form + '" action="' + option.url + '" enctype="multipart/form-data" target="' + option.target_uploader + '" class="file_upload">' + campos_extras + i_file + '<a title="Adicionar Midia" class="button left"><span class="icon icon189"></span><span class="label">Upload <img src="' + base_img_adm + 'images/ajax-loader.gif" class="img_load" style="display:none;" /></span></a></form><iframe id="' + option.target_uploader + '" name="' + option.target_uploader + '" src="" style="display: none; border:1px solid #000;" height="1" width="1" frameborder="0" scrolling="auto"></iframe>';
        $(obj).html(_formulario);
        start_upload(option, obj);
        if (option.debug_display == true) {
            $("#" + option.target_uploader).css({
                display: "block",
                height: "400px",
                width: "99%"
            });
        }
        function start_upload(option, obj) {
            $("#" + option.nome_form).find(".adicionar_arquivo").bind(($.browser.msie == true && $.browser.version < 9) ? "propertychange" : "change", function() {
                if ($(this).val() != "") {
                    $("#" + option.nome_form + " .img_load").show();
                    $("#" + option.nome_form).submit();
                    $("#" + option.nome_form + " .adicionar_arquivo").remove();
                    _retorno_upload_arquivo(option, obj);
                }
                return true;
            });
        }

        function _retorno_upload_arquivo(option, obj) {
            $("#" + option.target_uploader).load(function() {
                var contents = $.trim($(this).contents().find("body").html());
                var msg = "";
                switch (option.dataType) {
                    case"json":
                        try {
                            option.success.call(null, $.parseJSON(contents), obj);
                        } catch (Ex) {
                            option.error.call(null, $.parseJSON('{"cod":"404","msg":"Error: ' + Ex.name + "<br /> Descrição: " + Ex.message + '"}'), obj);
                        }
                        break;
                    case"xml":
                        try {
                            option.success.call(null, $.parseXML(contents), obj);
                        } catch (Ex) {
                            for (var v in Ex) {
                                msg += v + " = " + Ex[v] + "\n";
                            }
                            option.error.call(null, $.parseXML('<?xml version="1.0" encoding="UTF-8"?> <root> <cod>404</cod> <msg>Error: ' + Ex.name + "<br /> Descrição: " + Ex.message + "</msg> </root>"), obj);
                        }
                        break;
                    default:
                        try {
                            option.success.call(null, $.parseJSON(contents), obj);
                        } catch (Ex) {
                            option.error.call(null, $.parseJSON('{"cod":"404","msg":"Error: ' + Ex.name + "<br /> Descrição: " + Ex.message + '"}'), obj);
                        }
                        break;
                }
                $("#" + option.nome_form + " .img_load").hide();
                $("#" + option.nome_form).prepend(i_file);
                start_upload(option, obj);
                $(this).unbind("load");
            });
            return true;
        }

    }
});
